﻿using System;
using System.Web.Script.Serialization;
using ModuloSoporte.Excepciones;

namespace ModuloSoporte.Helpers
{
    public interface IAjaxResult
    {
        bool success { get;}
    }

    public class AjaxSuccess : IAjaxResult
    {
        public virtual bool success
        {
            get { return true; }
        }
    }

    public class AjaxError : IAjaxResult
    {
        public virtual bool success
        {
            get { return false; }
        }

        public int id;
        public string error;
        public string type;
        public string obj;

        public AjaxError()
            : this(0, "", AjaxHelper.Types.Exception, "")
        {

        }

        public AjaxError(int id, string message, string type)
            : this(0, "", AjaxHelper.Types.Exception, "")
        {

        }

        public AjaxError(int id, string error, string type, string obj)
        {
            this.id = id;
            this.error = error;
            this.type = type;
            this.obj = obj;
        }
    }

    public class ArrayAjaxSuccess : AjaxSuccess
    {
        public string[] values = null;

        public ArrayAjaxSuccess(string[] values)
        {
            this.values = values;
        }
    }

    public class ValueAjaxSuccess : AjaxSuccess
    {
        public string value = null;

        public ValueAjaxSuccess(string value)
        {
            this.value = value;
        }
    }

    public class AjaxHelper
    {
        public class Types
        {
            public static readonly string Message = "message";
            public static readonly string Exception = "exception";
        }

        public static string DoSuccess()
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(new AjaxSuccess());
        }

        public static string Do(object o)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(o);
        }

        public static string DoSuccess(params string[] values)
        {
            if (values.Length == 0)
                return DoSuccess();
            else
            {
                var js = new JavaScriptSerializer();
                AjaxSuccess obj;

                if (values.Length == 1)
                {
                    obj = new ValueAjaxSuccess(values[0]);
                }
                else
                {
                    obj = new ArrayAjaxSuccess(values);
                }
                return js.Serialize(obj);
            }
        }

        public static string DoSuccess(AjaxSuccess result)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(result);
        }

        public static string SendHtml(string htmlCode)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(new { success = true, html = htmlCode });
        }

        public static string DoError(AjaxError error)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(error);
        }

        public static string DoError(Exception ex)
        {
            if(ex is Excepcion)
                return DoError(new AjaxError(109, ex.Message, "excepcion", ex.ToString()));
            else
                return DoError(new AjaxError(100, ex.Message, "exception", ex.ToString()));
        }
        public static string DoError(string message)
        {
            return DoError(new AjaxError(101, message, "message", ""));
        }
        public static string DoError(int code, string message)
        {
            return DoError(new AjaxError(code, message, "message", ""));
        }
        public static string DoError()
        {
            return DoError(0, "");
        }
    }
}
